package com.khstudio.filebrowser.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class KhBitmapFactory {
    public static Bitmap decodeResource(Resources res, int id) {
        return BitmapFactory.decodeResource(res, id);
    }

    public static Bitmap createBitmap(Resources res, int id, int width, int height) {
        return Bitmap.createScaledBitmap(decodeResource(res, id), width, height, false);
    }

    public static Bitmap createBitmap(String filename) {
        Bitmap bitmap = null;

        try {
            bitmap = BitmapFactory.decodeFile(filename);
        } catch(OutOfMemoryError e) {
            boolean finish = true;
            int inSampleSize = 1;
            BitmapFactory.Options options = new BitmapFactory.Options();

            while(finish) {
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(filename, options );

                try{
                    inSampleSize++;
                    options.inSampleSize = inSampleSize;
                    options.inJustDecodeBounds = false;

                    bitmap = BitmapFactory.decodeFile(filename, options);
                    finish = false;
                } catch(OutOfMemoryError e1) {
                    Log.w("KhBitmapFactory",String.format("createBitmap OutOfMemoryError w = %d , h = %d , inSampleSize = %d", options.outWidth, options.outHeight , inSampleSize));
                }
            }
        }

        return bitmap;
    }

    public static Bitmap createBitmapOnRatio(String filename, int width, int height ) {
        Bitmap bitmap = createBitmap(filename);
        Bitmap b = null;

        if ( bitmap != null ) {
            b = createBitmapOnRatio(bitmap, width, height);
        }

        return b;
    }

    public static Bitmap createBitmapOnRatioFast(String filename, int width, int height ) {
        int inSampleSize = 1;
        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filename, options);

        double ratioWidth  = (double) options.outWidth  / (double) width;
        double ratioHeight = (double) options.outHeight / (double) height;

        if(ratioWidth > ratioHeight) {
            inSampleSize = (int) ratioWidth;
        } else {
            inSampleSize = (int) ratioHeight;
        }

        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = false;

        Bitmap bm = BitmapFactory.decodeFile(filename, options);
        Bitmap result = null;
        if(bm != null) {
            if(bm.getWidth() != width && bm.getHeight() != height) {
                result = createBitmapOnRatio(bm, width, height);
            } else {
                result = bm;
            }
        }

        return result;
    }

    public static Bitmap createBitmapOnRatio(Bitmap bitmap, int width, int height) {
        int modifyWidth = width;
        int modifyHeight = height;
        Bitmap bm = null;

        if(bitmap != null) {
            if(bitmap.getWidth() >= bitmap.getHeight()) {
                modifyHeight = (int) ((double) modifyWidth * (double) bitmap.getHeight() / (double) bitmap.getWidth());
            } else {
                modifyWidth = (int) ((double) modifyHeight * (double) bitmap.getWidth() / (double) bitmap.getHeight());
            }
            if(bitmap.getWidth() == modifyWidth && bitmap.getHeight() == modifyHeight) {
                bm = bitmap.copy(Bitmap.Config.ARGB_8888,true);
            } else {
                bm = Bitmap.createScaledBitmap(bitmap, modifyWidth, modifyHeight, false);
            }
        }
        bitmap.recycle();

        return bm;
    }

    public static Bitmap createBitmapOnFit(Bitmap bitmap, int width, int height) {
        Bitmap bm = null;

        if(bitmap.getWidth() == width && bitmap.getHeight() == height) {
            bm = bitmap;
        } else {
            bm = Bitmap.createScaledBitmap(bitmap, width, height, false);
            bitmap.recycle();
        }
        return bm;
    }


}
